package com.ruoyi.system.service.impl;

import java.util.HashMap;
import java.util.List;

import com.ruoyi.common.*;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.QRCodeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.QrcodeMapper;
import com.ruoyi.system.domain.Qrcode;
import com.ruoyi.system.service.IQrcodeService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;

/**
 * 二维码Service业务层处理
 *
 * @author ruoyi
 * @date 2025-05-07
 */
@Service
public class QrcodeServiceImpl implements IQrcodeService {
    @Resource
    private QrcodeMapper qrcodeMapper;
    @Autowired
    private MinioFileUtils minioFileUtils;

    /**
     * 查询二维码
     *
     * @param qrcodeId 二维码主键
     * @return 二维码
     */
    @Override
    public Qrcode selectQrcodeByQrcodeId(Long qrcodeId) {
        return qrcodeMapper.selectQrcodeByQrcodeId(qrcodeId);
    }

    /**
     * 查询二维码列表
     *
     * @param qrcode 二维码
     * @return 二维码
     */
    @Override
    public List<Qrcode> selectQrcodeList(Qrcode qrcode) {
        return qrcodeMapper.selectQrcodeList(qrcode);
    }

    /**
     * 新增二维码
     *
     * @param qrcode 二维码
     * @return 结果
     */
    @Override
    @Transactional
    public int insertQrcode(Qrcode qrcode) {
        qrcode.setCreateTime(DateUtils.getNowDate());

        //HashMap<Long, String> qrcodeMap = new HashMap<>();
        Qrcode update = null;
        for (int i = 0; i < qrcode.getNum(); i++) {
            qrcode.setQrcodeId(null);
            qrcodeMapper.insertQrcode(qrcode);
            long qid = qrcode.getQrcodeId();
            String hex=AES128Util.getAESEncodeHex(qid+"");
            if(StringUtils.hasLength(hex)) {
                String base64 = QRCodeUtils.getQRCodeBase64(hex);
                // qrcodeMap.put(qid, base64);
                Result result = minioFileUtils.uploadImg(base64);
                if (result.getCode() == Constant.SUCCESS_) {
                    FileProperty fileProperty = (FileProperty) result.getData();
                    update = new Qrcode();
                    update.setQrcodeId(qid);
                    update.setImagePath(fileProperty.getImagePath());
                    qrcodeMapper.updateQrcode(update);
                }
            }
        }

        return 1;
    }

    /**
     * 修改二维码
     *
     * @param qrcode 二维码
     * @return 结果
     */
    @Override
    public int updateQrcode(Qrcode qrcode) {
        qrcode.setUpdateTime(DateUtils.getNowDate());
        return qrcodeMapper.updateQrcode(qrcode);
    }

    /**
     * 批量删除二维码
     *
     * @param qrcodeIds 需要删除的二维码主键
     * @return 结果
     */
    @Override
    public int deleteQrcodeByQrcodeIds(Long[] qrcodeIds) {
        return qrcodeMapper.deleteQrcodeByQrcodeIds(qrcodeIds);
    }

    /**
     * 删除二维码信息
     *
     * @param qrcodeId 二维码主键
     * @return 结果
     */
    @Override
    public int deleteQrcodeByQrcodeId(Long qrcodeId) {
        return qrcodeMapper.deleteQrcodeByQrcodeId(qrcodeId);
    }
}
